package com.ptswitch.material.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hxzon.util.OgnlUtil;
import com.ptswitch.material.dao.HLogDao;
import com.ptswitch.material.model.Bom;
import com.ptswitch.material.model.HLog;
import com.ptswitch.material.model.Material;
import com.ptswitch.material.model.Uzer;
import com.ptswitch.material.service.HLogService;
import com.ptswitch.material.util.DateUtils;
@Service("hlogSer")
public class HLogServiceImpl extends LoggerService implements HLogService {
	@Resource
	private HLogDao hlogDao;
	
	public long countAll() {
		return hlogDao.countAll();
	}

	public long countLike(String like) {
		return hlogDao.countLike(like);
	}
	
	public void delete(String id){
		hlogDao.delete(id);
	}

	public void delete(HLog model) {
		hlogDao.delete(model);
	}
	
	public void deleteAll(){
		hlogDao.deleteAll();
	}
	
	public void deleteAll(List<HLog> models){
		hlogDao.deleteAll(models);
	}

	public HLog get(String id) {
		return hlogDao.get(id);
	}
	
	public List getAll(){
		return hlogDao.getAll();
	}

	public List getAll(long first, long size) {
		return hlogDao.getAll(first, size);
	}

	public List getLike(String like, long first, long size) {
		return hlogDao.getLike(like, first, size);
	}

	public void save(HLog model) {
		model.setCreateTime(DateUtils.format(new Date()));
		hlogDao.save(model);
	}
	
	public void saveAll(List<HLog> model) {
	}

	public long countLike(HLog model) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<HLog> getLike(HLog model, long first, long size) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String valid(HLog model){
		return null;
	}
	
	public void log(String username,String overview,String message){
		HLog log=new HLog();
		log.setUsername(username);
		log.setOverview(overview);
		if(message.length()>2048){
			message=message.substring(0,2048);
		}
		log.setMessage(message);
//		log.setCreateTime(DateUtils.format(new Date()));
		save(log);
	}
	
	public void log(Uzer user,String overview,String message){
		log(user.getUsername(),overview,message);
	}
	
	public void log(Uzer user,String overview,Object o){
		StringBuilder message=new StringBuilder();
		if (o != null) {
			for (String property : OgnlUtil.getAllStringProperty(o.getClass())) {
				message.append(property + ":" + OgnlUtil.getStringValueOrEmpty(o, property) + ";");
			}
		}
		log(user.getUsername(),overview,message.toString());
	}
	
	public void log(Uzer user,String overview){
		log(user,overview,(Object)null);
	}
	
	public String buildMessage(Material material){
//		overview+=material.getCode();
		StringBuilder message=new StringBuilder();
		message.append("物料编码:").append(material.getCode()).append(";\n");
		message.append("物料名称:").append(material.getName()).append(";\n");
		message.append("规格型号:").append(material.getSpecification()).append(";\n");
		message.append("器件封装:").append(material.getFootprint()).append(";\n");
		message.append("品牌:").append(material.getBrand()).append(";\n");
		message.append("器件描述:").append(material.getDescription()).append(";\n");
		message.append("供货商:").append(material.getSupplier()).append(";\n");
		message.append("pcb封装:").append(material.getPcbDecal()).append(";\n");
		message.append("备注:").append(material.getRemark()).append(";\n");
		message.append("临时编码:").append(material.getTmpCode()).append(";\n");
		message.append("采购状态:").append(material.getState()).append(";\n");
		return message.toString();
	}
	
	public String buildMessage(Bom bom){
//		overview+=bom.getCode();
		StringBuilder message=new StringBuilder();
		message.append("单板名:").append(bom.getCode()).append(";\n");
		message.append("版本:").append(bom.getVersion()).append(";\n");
		message.append("文件名:").append(bom.getFileName()).append(";\n");
		return message.toString();
	}
	
	public String buildMessage(Uzer userInfo){
//		overview+=userInfo.getUsername();
		StringBuilder message=new StringBuilder();
		message.append("用户名:").append(userInfo.getUsername()).append(";\n");
		message.append("用户等级:").append(userInfo.getAdminGrade()).append(";\n");
		return message.toString();
	}
}
